﻿using CNFM_CROCODILE_LOGO.Core.Domain.Current;
using CNFM_CROCODILE_LOGO.Core.Domain.Entity;
using CNFM_CROCODILE_LOGO.Core.Domain.ViewModels;
using CNFM_CROCODILE_LOGO.Core.Repository;
using CNFM_CROCODILE_LOGO.Data.RestClient;
using CNFM_CROCODILE_LOGO.RestClient.Domain;
using CNFM_CROCODILE_LOGO.RestClient.Utils;
using CNFM_CROCODILE_LOGO.Services.Interfaces;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CNFM_CROCODILE_LOGO.Services.Implementations
{
    /// <summary>
    /// 标识接口实现
    /// </summary>
    public class SignageServices : ISignageServices
    {
        private IEfRepository _rep;

        public SignageServices(IEfRepository rep)
        {
            this._rep = rep;
        }

        #region 标识入库
        /// <summary>
        /// 标识库存列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="status"></param>
        /// <param name="sasearch"></param>
        /// <returns></returns>
        public PaginationModel GetLogoInputStockAll(int index, int size, string status, string sasearch)
        {
            if (string.IsNullOrEmpty(status))
                status = "0";

            var list = _rep.Where<LogoStock>(p => true);

            var logoStockAll = list.OrderByDescending(p => p.CreateTime)
                .WhereIf(p => p.Status == Convert.ToInt32(status), string.IsNullOrEmpty(status) == false)
                .WhereIf(p => p.QRCode == sasearch
                || p.PackageNo == sasearch
                || p.LogoStockNo == sasearch, string.IsNullOrEmpty(sasearch) == false);
            var total = logoStockAll.Count();

            var models = logoStockAll.ToPaginationList(index, size)
              .ToList();
            return new PaginationModel { Code = 200, Msg = "success", Total = total, Result = new { Data = models } };
        }
        /// <summary>
        /// 标识库存添加
        /// </summary>
        /// <returns></returns>
        public ReturnValueModel AddLogoInputStock(List<LogoStockModels> models)
        {
            int ImportSum = models.Count();
            models.DistinctBy(p => p.QRCode);
            var list = _rep.Where<LogoStock>(p => p.Status == 0);
            List<LogoStock> logoStockAll = new List<LogoStock>();
            foreach (var item in models)
            {
                if (list.Where(p => p.QRCode == item.QRCode).Count() > 0)
                    continue;

                LogoStock logoStock = new LogoStock()
                {
                    Id = Guid.NewGuid().ToString(),
                    Status = 0,
                    CreateTime = DateTime.Now,
                    CreateId = UserBasic.UserId(),
                    Remarks = item.Remarks,
                    QRCode = item.QRCode,
                    LogoStockNo = item.LogoStockNo,
                    PackageNo = item.PackageNo,
                    Type = item.Type
                };
                logoStockAll.Add(logoStock);
            }

            _rep.BulkInsert(logoStockAll);

            return new ReturnValueModel { Code = 200, Msg = string.Format("导入:{0}条,成功:{1}条", ImportSum, logoStockAll.Count()) };
        }
        /// <summary>
        /// 条码分配
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public ReturnValueModel AllocationLogoInputStock(List<LogoStockModels> models, string sarId, string smId, string enId)
        {
            List<LogoStock> logoStocksAll = new List<LogoStock>();
            var list = _rep.Where<LogoStock>(p => p.Status == 0);
            foreach (var item in models)
            {
                if (list.Where(p => p.Id == item.Id).Count() == 0)
                    continue;
                var stockItem = list.FirstOrDefault(p => p.Id == item.Id);
                stockItem.EnterpriseId = enId;
                stockItem.SpeciesManageId = smId;
                stockItem.SignageApplicationReceiptId = sarId;
                stockItem.Status = 1;
                logoStocksAll.Add(stockItem);
            }

            _rep.BulkInsert(logoStocksAll);
            return new ReturnValueModel { Code = 200, Msg = "success!" };
        }
        /// <summary>
        /// 单个条码分配
        /// </summary>
        /// <param name="stockId"></param>
        /// <param name="sarId"></param>
        /// <param name="smId"></param>
        /// <param name="enId"></param>
        /// <returns></returns>
        public ReturnValueModel SingleDistributionInputStock(string qRCode, string sarId, string smId, string enId)
        {
            var stock = _rep.Where<LogoStock>(p => p.QRCode == qRCode);
            if (stock.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到该标识!" };
            if (stock.FirstOrDefault().Status != 0)
                return new ReturnValueModel { Code = 400, Msg = "该标识已被使用!" };

            var stockItem = stock.FirstOrDefault();
            stockItem.Status = 1;
            stockItem.SignageApplicationReceiptId = sarId;
            stockItem.SpeciesManageId = smId;
            stockItem.EnterpriseId = enId;
            _rep.Update(stockItem);

            return new ReturnValueModel { Code = 200, Msg = "success!" };
        }
        /// <summary>
        /// 批量码分配
        /// </summary>
        /// <param name="qRCodes"></param>
        /// <param name="sarId"></param>
        /// <param name="smId"></param>
        /// <param name="enId"></param>
        /// <returns></returns>
        public ReturnValueModel BatchDistributionInputStock(string qRCodes, string sarId, string smId, string enId)
        {
            var stockList = _rep.Where<LogoStock>(p => p.QRCode == qRCodes);
            if (stockList.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到可用标识!" };

            var stockAll = stockList.ToList();
            foreach (var item in stockAll)
            {
                item.Status = 1;
                item.SignageApplicationReceiptId = sarId;
                item.SpeciesManageId = smId;
                item.EnterpriseId = enId;
            }

            _rep.BulkInsert(stockAll);
            return new ReturnValueModel { Code = 200, Msg = "success!" };
        }
        #endregion

        #region 产品类别
        /// <summary>
        /// 产品列表
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public ReturnValueModel GetSpeciesManageTypeAll(string type)
        {
            if (string.IsNullOrEmpty(type))
                return new ReturnValueModel { Code = 400, Msg = "请求格式错误!" };

            var manageList = from a in _rep.Where<SpeciesManage>(p => p.Status == 0)
                             join d in _rep.Where<Dictionary>(p => p.Status == 0) on a.Type equals d.Id
                             select new
                             {
                                 a.Id,
                                 a.SpeciesName,
                                 Type = d.Id
                             };

            var manageAll = manageList
                .WhereIf(p => p.Type == type, string.IsNullOrEmpty(type) == false)
                .ToList();

            return new ReturnValueModel { Code = 200, Msg = "success!", Result = new { Data = manageAll } };
        }
        #endregion

        #region 标识申请单
        /// <summary>
        /// 标识申请单新增修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnValueModel AddUpdateSignageApplication(SignageApplicationReceiptModels model)
        {
            List<string> mgsList = new List<string>();
            mgsList.Add(model.ApplyNum == 0 ? "申请数量" : null);
            mgsList.Add((string.IsNullOrEmpty(model.SpeciesId) ? "产品" : null));
            mgsList.Add((string.IsNullOrEmpty(model.Person) ? "申请人" : null));
            foreach (var item in mgsList)
                if (!string.IsNullOrEmpty(item))
                    return new ReturnValueModel { Code = 400, Msg = String.Format("您的{0}没有填写!", item) };

            var enpId = _rep.FirstOrDefault<CrocodileUser>(p => p.Id == UserBasic.UserId()).EnterpriseId;

            if (string.IsNullOrEmpty(model.Id))
            {
                SignageApplicationReceipt item = new SignageApplicationReceipt()
                {
                    Id = Guid.NewGuid().ToString(),
                    Status = 0,
                    CreateId = UserBasic.UserId(),
                    CreateTime = DateTime.Now,
                    Remarks = model.Remarks,
                    SignageApplicationNo = string.Format("{0}{1}", UtilsHelp.GetTimeStamp(), UtilsHelp.getRandNum(4)),
                    RegisteredTime = model.RegisteredTime,
                    Person = model.Person,
                    EnterpriseId = enpId,
                    ApplyNum = model.ApplyNum,
                    SpeciesId = model.SpeciesId,
                    Details = model.Details,
                    Purpose = model.Purpose
                };

                _rep.Insert(item);
                return new ReturnValueModel { Code = 200, Msg = "success!" };
            }

            var signageAplList = _rep.Where<SignageApplicationReceipt>(p => p.Status == 0 && p.Id == model.Id).ToList();
            if (signageAplList.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到该申请单,请确认是否为可用状态!" };

            var signageApItem = signageAplList.FirstOrDefault();
            signageApItem.RegisteredTime = model.RegisteredTime;
            signageApItem.Person = model.Person;
            signageApItem.ApplyNum = model.ApplyNum;
            signageApItem.SpeciesId = model.SpeciesId;
            signageApItem.Purpose = model.Purpose;
            signageApItem.Details = string.IsNullOrEmpty(model.Details) ? signageApItem.Details : model.Details;
            signageApItem.Remarks = string.IsNullOrEmpty(model.Remarks) ? signageApItem.Remarks : model.Remarks;

            _rep.Update(signageApItem);
            return new ReturnValueModel { Code = 200, Msg = "success!" };
        }

        /// <summary>
        /// 标识申请单删除
        /// </summary>
        /// <param name="signagId"></param>
        /// <returns></returns>
        public ReturnValueModel DeleteSignageApplication(string signagId)
        {
            if (string.IsNullOrEmpty(signagId))
                return new ReturnValueModel { Code = 400, Msg = "申请单Id,不能为空!" };
            var signageApplicationList = _rep.Where<SignageApplicationReceipt>(p => p.Status == 0 && p.Id == signagId).ToList();
            if (signageApplicationList.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到该详情,请确认是否为可用状态!" };

            var signageApplicationItem = signageApplicationList.FirstOrDefault();
            signageApplicationItem.Status = -1;
            _rep.Update(signageApplicationItem);

            return new ReturnValueModel { Code = 200, Msg = "success!" };
        }

        /// <summary>
        /// 标识申请单详情
        /// </summary>
        /// <param name="signagId"></param>
        /// <returns></returns>
        public ReturnValueModel GetSignageApplication(string signagId)
        {
            if (string.IsNullOrEmpty(signagId))
                return new ReturnValueModel { Code = 400, Msg = "申请单Id,不能为空!" };

            var signageApplicationList = _rep.Where<SignageApplicationReceipt>(p => p.Status != -1 && p.Id == signagId).ToList();
            if (signageApplicationList.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到该详情,请确认是否为可用状态!" };

            var model = signageApplicationList.FirstOrDefault();

            return new ReturnValueModel { Code = 200, Msg = "success!", Result = new { Data = model } };
        }

        /// <summary>
        /// 标识申请单列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="areUnCode"></param>
        /// <param name="status"></param>
        /// <param name="sasearch"></param>
        /// <returns></returns>
        public PaginationModel GetSignageApplicationAll(int index, int size, string areUnCode, string enterpriseId, string status, string sasearch)
        {
            //地区数据
            var sql1 = string.Empty;
            List<string> arrt3 = new List<string>();
            string[] arrt4;
            if (!string.IsNullOrEmpty(areUnCode) && areUnCode != "undefined" && areUnCode != "null")
            {
                sql1 = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE YUnCode={0} AND Status=0 UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                var areaAll = _rep.SqlQuery<Area>(sql1, areUnCode).ToList();

                foreach (var item in areaAll)
                    arrt3.Add(item.YUnCode);
                arrt4 = arrt3.ToArray();
            }
            else
                arrt4 = null;

            var stockAll = _rep.Where<LogoStock>(p => p.Status != 0 && p.Status != -1);
            var signageApplicationList = from a in _rep.Where<SignageApplicationReceipt>(p => true)
                                         where string.IsNullOrEmpty(status) && status == "undefined" && status == "null" ? a.Status != -1 :
                                         (status == "5" ? a.Status != -1 && a.Status != -2 && a.Status != 4 : a.Status == Convert.ToInt32(status))
                                         join e in _rep.Where<Enterprise>(p => p.Status == 0) on a.EnterpriseId equals e.Id
                                         where !string.IsNullOrEmpty(areUnCode) && areUnCode != "undefined" && areUnCode != "null"
                                            ? arrt4.Contains(e.UnCode) : e.Status == 0
                                         join s in _rep.Where<SpeciesManage>(p => p.Status == 0) on a.SpeciesId equals s.Id
                                         join d in _rep.Where<Dictionary>(p => p.Status == 0) on s.Type equals d.Id
                                         join u in _rep.Where<CrocodileUser>(p => p.Status == 0) on a.CreateId equals u.Id
                                         select new
                                         {
                                             a.Id,
                                             a.Remarks,
                                             a.Status,
                                             a.CreateId,
                                             a.RegisteredTime,
                                             a.Person,
                                             a.EnterpriseId,
                                             a.ApplyNum,
                                             a.OneToExamineNum,
                                             a.TwoToExamineNum,
                                             ThreeToExamineNum = stockAll.Where(p => p.SignageApplicationReceiptId == a.Id).Count(),
                                             a.OneRemarks,
                                             a.TwoRemarks,
                                             a.SpeciesId,
                                             a.Details,
                                             a.Purpose,
                                             CreateName = u.UserName,
                                             e.EnterpriseName,
                                             s.SpeciesName
                                         };

            var sql = signageApplicationList.ToSql();

            var signageApplicationAll = signageApplicationList.OrderByDescending(p => p.RegisteredTime)
                .WhereIf(p => p.EnterpriseName.Contains(sasearch) || p.SpeciesName.Contains(sasearch)
                || p.CreateName.Contains(sasearch) || p.Purpose.Contains(sasearch), string.IsNullOrEmpty(sasearch) == false)
                .WhereIf(p => p.EnterpriseId == enterpriseId, string.IsNullOrEmpty(enterpriseId) == false);
            var total = signageApplicationAll.Count();

            var models = signageApplicationAll.ToPaginationList(index, size)
                .ToList();
            return new PaginationModel { Code = 200, Msg = "success", Total = total, Result = new { Data = models } };
        }

        /// <summary>
        /// 标识申请单初审
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnValueModel ReviewSignageApplicationOne(SignageApplicationReview model)
        {
            if (string.IsNullOrEmpty(model.Id))
                return new ReturnValueModel { Code = 400, Msg = "申请单Id,不能为空!" };
            if (model.OneToExamineNum == 0 && model.IsReview == true)
                return new ReturnValueModel { Code = 400, Msg = "审批数量不能为0!" };
            if (model.OneToExamineNum > model.ApplyNum)
                return new ReturnValueModel { Code = 400, Msg = "审批数量不能大于申请数量!" };
            var signageApplicationList = _rep.Where<SignageApplicationReceipt>(p => p.Status != -1 && p.Id == model.Id).ToList();
            if (signageApplicationList.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到该详情,请确认是否为可用状态!" };

            var signageApplicationItem = signageApplicationList.FirstOrDefault();
            if (model.IsReview == true)
                signageApplicationItem.Status = 2;
            else
                signageApplicationItem.Status = -2;

            signageApplicationItem.OneToExamineNum = model.OneToExamineNum;
            signageApplicationItem.OneRemarks = model.Remarks;
            _rep.Update(signageApplicationItem);
            return new ReturnValueModel { Code = 200, Msg = "success!" };
        }

        /// <summary>
        /// 标识申请单复审
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnValueModel ReviewSignageApplicationTwo(SignageApplicationReview model)
        {
            if (string.IsNullOrEmpty(model.Id))
                return new ReturnValueModel { Code = 400, Msg = "申请单Id,不能为空!" };
            if (model.TwoToExamineNum == 0 && model.IsReview == true)
                return new ReturnValueModel { Code = 400, Msg = "复审数量不能为0!" };
            if (model.TwoToExamineNum > model.OneToExamineNum)
                return new ReturnValueModel { Code = 400, Msg = "复审数量不能大于初审数量!" };
            var signageApplicationList = _rep.Where<SignageApplicationReceipt>(p => p.Status != -1 && p.Id == model.Id).ToList();
            if (signageApplicationList.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到该详情,请确认是否为可用状态!" };

            var signageApplicationItem = signageApplicationList.FirstOrDefault();
            if (model.IsReview == true)
                signageApplicationItem.Status = 3;
            else
                signageApplicationItem.Status = -2;

            signageApplicationItem.TwoToExamineNum = model.TwoToExamineNum;
            signageApplicationItem.OneRemarks = model.Remarks;
            _rep.Update(signageApplicationItem);
            return new ReturnValueModel { Code = 200, Msg = "success!" };
        }
        #endregion

        #region 标识牌管理
        /// <summary>
        /// 标识牌列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="enterpriseId"></param>
        /// <param name="speciesManageId"></param>
        /// <param name="status"></param>
        /// <param name="sasearch"></param>
        /// <returns></returns>
        public PaginationModel GetIdentificationAll(int index, int size, string enterpriseId, string areUnCode, string speciesManageId, string status, string sasearch)
        {
            //地区数据
            var sql1 = string.Empty;
            List<string> arrt3 = new List<string>();
            string[] arrt4;
            if (!string.IsNullOrEmpty(areUnCode) && areUnCode != "undefined" && areUnCode != "null")
            {
                sql1 = "WITH CTE AS ( SELECT YUnCode,PUnCode FROM dbo.Area WHERE YUnCode={0} AND Status=0 UNION ALL SELECT a.YUnCode,a.PUnCode FROM dbo.Area a INNER JOIN CTE b ON b.YUnCode=a.PUnCode) SELECT a.*  FROM dbo.Area a INNER JOIN CTE t ON a.YUnCode=t.YUnCode";
                var areaAll = _rep.SqlQuery<Area>(sql1, areUnCode).ToList();

                foreach (var item in areaAll)
                    arrt3.Add(item.YUnCode);
                arrt4 = arrt3.ToArray();
            }
            else
                arrt4 = null;

            var logoStockList = from a in _rep.Where<LogoStock>(p => p.Status != 0)
                                join s in _rep.Where<SignageApplicationReceipt>(p => p.Status != -1) on a.SignageApplicationReceiptId equals s.Id
                                join m in _rep.Where<SpeciesManage>(p => p.Status == 0) on a.SpeciesManageId equals m.Id
                                join d in _rep.Where<Dictionary>(p => p.Status == 0) on m.Type equals d.Id
                                join e in _rep.Where<Enterprise>(p => p.Status == 0) on a.EnterpriseId equals e.Id
                                where !string.IsNullOrEmpty(areUnCode) && areUnCode != "undefined" && areUnCode != "null"
                                            ? arrt4.Contains(e.UnCode) : e.Status == 0
                                select new
                                {
                                    a.Id,
                                    a.CreateTime,
                                    a.SignageApplicationReceiptId,
                                    s.SignageApplicationNo,
                                    a.EnterpriseId,
                                    e.EnterpriseName,
                                    a.SpeciesManageId,
                                    TypeName = d.Name,
                                    m.SpeciesName,
                                    a.LogoStockNo,
                                    a.QRCode,
                                    a.PackageNo,
                                    a.Type,
                                    a.Remarks,
                                    a.Status
                                };


            var logoStockAll = logoStockList.OrderByDescending(p => p.CreateTime)
               .WhereIf(p => p.LogoStockNo.Contains(sasearch)
               || p.QRCode.Contains(sasearch)
               || p.SignageApplicationNo.Contains(sasearch)
               || p.EnterpriseName.Contains(sasearch)
               || p.PackageNo.Contains(sasearch), string.IsNullOrEmpty(sasearch) == false)
               .WhereIf(p => p.EnterpriseId == enterpriseId, string.IsNullOrEmpty(enterpriseId) == false)
               .WhereIf(p => p.SpeciesManageId == speciesManageId, string.IsNullOrEmpty(speciesManageId) == false)
               .WhereIf(p => p.Status == Convert.ToInt32(status), string.IsNullOrEmpty(status) == false);
            var total = logoStockAll.Count();

            var models = logoStockAll.ToPaginationList(index, size)
                .ToList();
            return new PaginationModel { Code = 200, Msg = "success", Total = total, Result = new { Data = models } };
        }
        /// <summary>
        /// 标识牌详情
        /// </summary>
        /// <param name="identifId"></param> 
        /// <returns></returns>
        public ReturnValueModel GetIdentification(string identifId)
        {
            if (string.IsNullOrEmpty(identifId))
                return new ReturnValueModel { Code = 400, Msg = "标识Id,不能为空!" };

            var identificationList = _rep.Where<Identification>(p => p.Status != -1 && p.Id == identifId).ToList();
            if (identificationList.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到该详情,请确认是否为可用状态!" };

            var model = identificationList.FirstOrDefault();

            return new ReturnValueModel { Code = 200, Msg = "success!", Result = new { Data = model } };
        }
        /// <summary>
        /// 标识牌导入
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnValueModel AddIdentification(IdentificationModels model)
        {
            List<string> mgsList = new List<string>();
            mgsList.Add((string.IsNullOrEmpty(model.SpeciesManageId) ? "产品" : null));
            mgsList.Add((string.IsNullOrEmpty(model.SignageApplicationReceiptId) ? "申请单Id" : null));
            foreach (var item in mgsList)
                if (!string.IsNullOrEmpty(item))
                    return new ReturnValueModel { Code = 400, Msg = String.Format("您的{0}没有填写!", item) };

            if (model.IdentificationList.Count() == 0)
                return new ReturnValueModel { Code = 400, Msg = "标识二维码不能为空!" };

            var signageApplicationReceiptItem = _rep.FirstOrDefault<SignageApplicationReceipt>(p => p.Id == model.SignageApplicationReceiptId);
            if (string.IsNullOrEmpty(signageApplicationReceiptItem.Id))
                return new ReturnValueModel { Code = 400, Msg = "查找不到申请单!" };
            if ((signageApplicationReceiptItem.ThreeToExamineNum + model.IdentificationList.Count()) > signageApplicationReceiptItem.TwoToExamineNum)
                return new ReturnValueModel { Code = 400, Msg = "标识导入数量不能大于审批数量!" };

            var total = _rep.Where<SignageApplicationReceipt>(p => p.Id == model.SignageApplicationReceiptId).Count();
            var idfList = _rep.Where<Identification>(p => p.Status != -1);


            model.IdentificationList.DistinctBy(p => p.QRCode);

            List<Identification> identificationAll = new List<Identification>();
            foreach (var item in model.IdentificationList)
            {
                if (idfList.Where(p => p.QRCode == item.QRCode).Count() > 1)
                    continue;
                Identification identification = new Identification()
                {
                    Id = Guid.NewGuid().ToString(),
                    Status = 0,
                    CreateTime = DateTime.Now,
                    CreateId = UserBasic.UserId(),
                    Remarks = model.Remarks,
                    RegisteredTime = DateTime.Now,
                    SignageApplicationReceiptId = model.SignageApplicationReceiptId,
                    EnterpriseId = model.EnterpriseId,
                    SpeciesManageId = model.SpeciesManageId,
                    QRCode = item.QRCode,
                    IdentificationNo = string.Format("{0}{1}", signageApplicationReceiptItem.SignageApplicationNo, total + 1)
                };
                identificationAll.Add(identification);
            }

            var addTotal = identificationAll.Count();

            _rep.BulkInsert(identificationAll);

            if ((signageApplicationReceiptItem.ThreeToExamineNum + model.IdentificationList.Count()) == signageApplicationReceiptItem.TwoToExamineNum)
                signageApplicationReceiptItem.Status = 4;
            else
                signageApplicationReceiptItem.ThreeToExamineNum = signageApplicationReceiptItem.ThreeToExamineNum + addTotal;

            _rep.Update(signageApplicationReceiptItem);

            return new ReturnValueModel { Code = 200, Msg = "success!", Result = new { Data = addTotal } };
        }
        /// <summary>
        /// 标识牌激活
        /// </summary>
        /// <param name="identifId"></param>
        /// <returns></returns>
        public ReturnValueModel UpdateIdentification(string identifId)
        {
            if (string.IsNullOrEmpty(identifId))
                return new ReturnValueModel { Code = 400, Msg = "标识Id,不能为空!" };
            var logoStockList = _rep.Where<LogoStock>(p => p.Status == 1 && p.Id == identifId).ToList();
            if (logoStockList.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到该标识,请确认是否为可用状态!" };

            var logoStockItem = logoStockList.FirstOrDefault();
            logoStockItem.Status = 2;
            _rep.Update(logoStockItem);

            return new ReturnValueModel { Code = 200, Msg = "success!" };
        }

        /// <summary>
        /// 标识牌激活(单个)
        /// </summary>
        /// <param name="qrCode"></param>
        /// <returns></returns>
        public ReturnValueModel UpdateSpecifyActivation(string qrCode)
        {
            if (string.IsNullOrEmpty(qrCode))
                return new ReturnValueModel { Code = 400, Msg = "标识二维码,不能为空!" };
            var logoStockList = _rep.Where<LogoStock>(p => p.Status == 1 && p.QRCode == qrCode).ToList();
            if (logoStockList.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到该标识,请确认是否为可用状态!" };

            var logoStockItem = logoStockList.FirstOrDefault();
            logoStockItem.Status = 2;
            _rep.Update(logoStockItem);

            return new ReturnValueModel { Code = 200, Msg = "success!" };
        }

        /// <summary>
        /// 标识牌激活(批量)
        /// </summary>
        /// <param name="qrCodes"></param>
        /// <returns></returns>
        public ReturnValueModel UpdateSpecifyActivationAll(string qrCodes)
        {
            if (string.IsNullOrEmpty(qrCodes))
                return new ReturnValueModel { Code = 400, Msg = "批量码,不能为空!" };
            var logoStockList = _rep.Where<LogoStock>(p => p.Status == 1 && p.PackageNo == qrCodes).ToList();
            if (logoStockList.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到可用标识!" };

            foreach (var item in logoStockList)
                item.Status = 2;

            _rep.BulkInsert(logoStockList);
            return new ReturnValueModel { Code = 200, Msg = "success!" };
        }

        /// <summary>
        /// 标识多选激活
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public ReturnValueModel UpdateActivationAll(List<LogoStockModels> models)
        {
            if(models.Count()==0)
                return new ReturnValueModel { Code = 400, Msg = "请选择需要激活的标识!" };

            List<LogoStock> logoStocksAll = new List<LogoStock>();
            var list = _rep.Where<LogoStock>(p => p.Status == 1);
            foreach (var item in models)
            {
                if(string.IsNullOrEmpty(item.Id))
                    continue;
                if (list.Where(p => p.Id == item.Id).Count() == 0)
                    continue;
                var stockItem = list.FirstOrDefault(p => p.Id == item.Id);
                stockItem.Status = 2;
                logoStocksAll.Add(stockItem);
            }

            _rep.BulkInsert(logoStocksAll);
            return new ReturnValueModel { Code = 200, Msg = "success!" };
        }
        #endregion
    }
}
